﻿using System;
using System.Windows.Forms;
using System.IO;
using System.Drawing;
using MG_POS_PC.Forms.Setting;


namespace MG_POS_PC
{
    public partial class MainForm : BaseForm
    {
        private Form defaultSubForm;
        private Form networkSetting;
        private Form userSetting;
        private Form t_user_setting_form;
        //private Form networkReport;

        public MainForm()
        {
            Form loginForm = new LoginForm();
            var result = loginForm.ShowDialog();
            if (result != System.Windows.Forms.DialogResult.OK)
            {
                System.Environment.Exit(0);
            }
            InitializeComponent();
            skinEngine1.SkinStream = memoryStreams[themeId];
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            defaultSubForm = new NetworkSetting();
            networkSetting = defaultSubForm;
            t_user_setting_form = new MG_POS_PC.Forms.Setting.t_user_setting_form();
            
            userSetting = new UserSetting();
            GetMenu(treeView1, menuStrip2);
            setFuncForm(defaultSubForm);
        }

        public void reflesh()
        {
            skinEngine1.SkinStream = memoryStreams[themeId];
        }

        private void fileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setFuncForm(networkSetting);
        }

        private void sToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //setFuncForm(networkReport);
            setFuncForm(t_user_setting_form);
        }

        private void userSettingToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setFuncForm(userSetting);
        }

        private void setFuncForm(Form form)
        {
            splitContainer1.Panel2.Controls.Clear();
            form.TopLevel = false;
            form.Dock = DockStyle.Fill;
            form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            splitContainer1.Panel2.Controls.Add(form);
            form.Show();
        }

        public void GetMenu(TreeView treeV, MenuStrip MenuS)
        {
            bool Var_Bool = true;
            for (int i = 0; i < MenuS.Items.Count; i++)
            {
                TreeNode newNode1 = treeV.Nodes.Add(MenuS.Items[i].Text);
                if (MenuS.Items[i].Enabled == false)
                {
                    newNode1.ForeColor = Color.Silver;
                    Var_Bool = false;
                }
                else
                {
                    newNode1.ForeColor = Color.Black;
                    Var_Bool = true;
                }
                newNode1.Tag = 0;
                ToolStripDropDownItem newmenu = (ToolStripDropDownItem)MenuS.Items[i];
                GetCavernMenu(newNode1, newmenu, Var_Bool);
            }
        }

        public void GetCavernMenu(TreeNode newNodeA, ToolStripDropDownItem newmenuA, bool BL)
        {
            bool Var_Bool = true;
            if (newmenuA.HasDropDownItems && newmenuA.DropDownItems.Count > 0)
                for (int j = 0; j < newmenuA.DropDownItems.Count; j++)
                {
                    TreeNode newNodeB = newNodeA.Nodes.Add(newmenuA.DropDownItems[j].Text);
                    Var_Bool = true;
                    if (BL == false)
                    {
                        newNodeB.ForeColor = Color.Silver;
                        newNodeB.Tag = 0;
                        Var_Bool = false;
                    }
                    else
                    {
                        if (newmenuA.DropDownItems[j].Enabled == false)
                        {
                            newNodeB.ForeColor = Color.Silver;
                            newNodeB.Tag = 0;
                            Var_Bool = false;
                        }
                        else
                        {
                            newNodeA.ForeColor = Color.Black;
                            newNodeB.Tag = 1;
                            Var_Bool = true;
                        }
                    }

                    ToolStripDropDownItem newmenuB = (ToolStripDropDownItem)newmenuA.DropDownItems[j];
                    if (newmenuB.HasDropDownItems && newmenuA.DropDownItems.Count > 0)
                    {
                        newNodeB.Tag = 0;
                        GetCavernMenu(newNodeB, newmenuB, Var_Bool);
                    }
                }
        }
        #region setTheme
        private void xPGreenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setTheme(5);
        }
        private void xPSilverToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setTheme(4);
        }
        private void vistaGreenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setTheme(3);
        }
        private void calmnessToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setTheme(0);
        }
        private void macOSToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setTheme(1);
        }
        private void vIstaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setTheme(2);
        }
        private void setTheme(int themeid)
        {
            themeId = themeid;
            reflesh();
        }
        #endregion

        public static void writeLog(string mess)
        {
            FileStream fs = new FileStream(@"c:/mcWindowsService.txt", FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter m_streamWriter = new StreamWriter(fs);
            m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
            m_streamWriter.WriteLine(" -->" + mess);
            m_streamWriter.Flush();
            m_streamWriter.Close();
            fs.Close();
        }
    }
}
